<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 满减券
 *
 * @icon fa fa-circle-o
 */
class Coupons extends Backend
{

    /**
     * Coupons模型对象
     * @var \app\admin\model\user\Coupons
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\Coupons;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','title','full','reduces','num','sort','type','create_time']);
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /*
     * 发送优惠券 用户id,优惠券id
     * */
    public function sendCoup()
    {
        $data = $this->request->param();
        $user_id = implode('，',$data['user_id']);
        $res  = [];
        foreach($user_id as $val)
        {
            $res[] = ['num'=>$data['num'],'user_id'=>$val,'cou_id'=>$data['coup_id'],'create_time'=>time()];
        }

        $result = Db::name('user_coupons')->insertAll($res);
        if($result)
        {
            return ['status'=>'1','message'=>'成功'];
        }else{
            return ['status'=>'2','message'=>'失败'];
        }
    }

    /*
     * 添加发送满减券
     * */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                
                if(!$params['title'])$this->error('请输入标题');
                if(!$params['full'])$this->error('请输入满值');
                if(!$params['reduces'])$this->error('请输入减值');
                if(!$params['num'])$this->error('请输入数量');
                if(strtotime($params['start_time']) > strtotime($params['end_time']))
                    $this->error('开始时间不能大于结束时间');

                $data = ['title'=>$params['title'],'sort'=>$params['sort'],'full'=>$params['full'],'reduces'=>$params['reduces'],'num'=>$params['num'],'start_time'=>strtotime($params['start_time']),'end_time'=>strtotime($params['end_time']),'create_time'=>time()];
                $res = Db::name('coupons')->insert($data);
                
                if($res)
                {
                    $this->success();
                }else{
                    $this->error();
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }


    /*
     * 修改发送满减券
     * */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if(!$params['title'])$this->error('请输入标题');
                if(!$params['full'])$this->error('请输入满值');
                if(!$params['reduces'])$this->error('请输入减值');
                if(!$params['num'])$this->error('请输入数量');
                if(strtotime($params['start_time']) > strtotime($params['end_time']))
                    $this->error('开始时间不能大于结束时间');

                $data = ['title'=>$params['title'],'sort'=>$params['sort'],'full'=>$params['full'],'reduces'=>$params['reduces'],'num'=>$params['num'],'start_time'=>strtotime($params['start_time']),'end_time'=>strtotime($params['end_time']),'update_time'=>time()];
                
                $res = Db::name('coupons')->where('id',$ids)->update($data);

                if($res)
                {
                    $this->success();
                }else{
                    $this->error();
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row",$row);
        return $this->view->fetch();
    }
}
